package cn.seaboot.admin.file.web;

import cn.seaboot.admin.file.bean.SysFile;
import cn.seaboot.admin.file.service.FileService;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.argument.Params;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 文件上传/预览/下载
 *
 * @author Mr.css on 2018-6-20
 */
@Controller
@Tag(name = "系统文件 [t_sys_file] 管理")
@RequestMapping("sys/file")
public class FileCtrl {

    @Resource
    private FileService fileService;

    /**
     * 保存文件，并且返回文件信息
     *
     * @param type 文件类型，与存储位置挂钩，主要是为了方便组织业务逻辑
     * @param file 文件实体
     * @return 文件存储信息
     * @throws IOException -
     */
    @ResponseBody
    @Operation(description = "上传文件")
    @RequestMapping(value = "upload/{type}", method = RequestMethod.POST)
    @Parameters({@Parameter(name = "type", description = "文件类型")})
    public SysFile uploadToFolder(@PathVariable String type, MultipartFile file) throws IOException {
        return fileService.saveFile(type, file);
    }

    /**
     * 文件下载
     *
     * @param id 文件ID
     */
    @ResponseBody
    @Operation(description = "系统文件 [t_sys_file] 下载")
    @Parameters({@Parameter(name = "id", description = "文件ID")})
    @RequestMapping(value = "download/{id}", method = RequestMethod.GET)
    public void download(@PathVariable String id
            , HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileService.download(id, request, response);
    }

    /**
     * 文件预览
     *
     * @param id 文件ID
     */
    @ResponseBody
    @Operation(description = "系统文件 [t_sys_file] 预览")
    @Parameters({@Parameter(name = "id", description = "文件ID")})
    @RequestMapping(value = "preview/{id}", method = RequestMethod.GET)
    public void preview(@PathVariable String id
            , HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileService.preview(id, request, response);
    }

    /**
     * 删除
     *
     * @param id id
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "系统文件 [t_sys_file] 删")
    @Parameters({@Parameter(name = "id", description = "ID")})
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params String id) throws IOException {
        return fileService.deleteById(id);
    }


    /**
     * /**
     * 查询列表
     *
     * @return PageInfo
     */
    @ResponseBody
    @Operation(description = "系统文件 [t_sys_file] 列表查询")
    @Parameters({
            @Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数")})
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<SysFile> queryPage(@RequestParam Map<String, Object> params) {
        return Pagination.apply(params, map-> fileService.queryList(map));
    }
}
